﻿using WindowsForms.AOnline.Model.BusinessObjects.Dictionary;
using WindowsForms.AOnline.View.Dictionary;
//using System.Threading.Tasks;

namespace WindowsForms.AOnline.Presenter.Dictionary.InventoryItem
{
    public class InventoryItemPresenter : Presenter<IInventoryItemView>  
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="InventoryItemPresenter" /> class.
        /// </summary>
        /// <param name="view">The view.</param>
        public InventoryItemPresenter(IInventoryItemView view)
            : base(view)
        {
        }

        /// <summary>
        /// Displays the specified employ identifier.
        /// </summary>
        /// <param name="inventoryItemId">The employ identifier.</param>
        public void Display(string inventoryItemId) 
        {
            if (inventoryItemId == null) { View.InventoryItemId = 0; return; }
            var inventoryItem = Model.GetInventoryItem(int.Parse(inventoryItemId));
            View.InventoryItemId = inventoryItem.InventoryItemId;
            View.AccountCode = inventoryItem.AccountCode;
            View.InventoryItemCode = inventoryItem.InventoryItemCode;
            View.InventoryItemName = inventoryItem.InventoryItemName;
            View.IsActive = inventoryItem.IsActive;
            View.Unit = inventoryItem.Unit;
            View.UnitId = inventoryItem.UnitId;
        }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        /// <returns>System.Int32.</returns>
        public string Save()
        {
            var obj = new InventoryItemModel
            {
                InventoryItemCode = View.InventoryItemCode,
                InventoryItemId = View.InventoryItemId,
                InventoryItemName = View.InventoryItemName,
                AccountCode = View.AccountCode,
                IsActive = View.IsActive,
                Unit = View.Unit,
                UnitId = View.UnitId,
            };
            return View.InventoryItemId == 0 ? Model.AddInventoryItem(obj) : Model.UpdateInventoryItem(obj);
        }

        /// <summary>
        /// Deletes the specified employ identifier.
        /// </summary>
        /// <param name="inventoryItemId">The employ identifier.</param>
        /// <returns>System.Int32.</returns>
        public string Delete(int inventoryItemId) 
        {
            return Model.DeleteInventoryItem(inventoryItemId);
        }
    }
}
